import React from 'react'
import ReactDOM from 'react-dom'

import SearchCom from './assects/js/searchCom'

import yiqing from './yiqing.json'
import echarts from 'echarts'
import 'echarts/map/js/china'
import './index.css'

// 制作疫情地图
// https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=322449874851

// console.log(yiqing)
// console.log(yiqing.data)
// console.log(yiqing.data.areaTree)
console.log(yiqing.data.areaTree[2])

// total:
// confirm: 942
// dead: 9
// heal: 893
// input: 0
// severe: 0
// suspect: 0
const dataList = [
  { name: "南海诸岛", value: 0 },
  { name: '北京', value: randomValue() },
  { name: '天津', value: randomValue() },
  { name: '上海', value: randomValue() },
  { name: '重庆', value: randomValue() },
  { name: '河北', value: randomValue() },
  { name: '河南', value: randomValue() },
  { name: '云南', value: randomValue() },
  { name: '辽宁', value: randomValue() },
  { name: '黑龙江', value: randomValue() },
  { name: '湖南', value: randomValue() },
  { name: '安徽', value: randomValue() },
  { name: '山东', value: randomValue() },
  { name: '新疆', value: randomValue() },
  { name: '江苏', value: randomValue() },
  { name: '浙江', value: randomValue() },
  { name: '江西', value: randomValue() },
  { name: '湖北', value: randomValue() },
  { name: '广西', value: randomValue() },
  { name: '甘肃', value: randomValue() },
  { name: '山西', value: randomValue() },
  { name: '内蒙古', value: randomValue() },
  { name: '陕西', value: randomValue() },
  { name: '吉林', value: randomValue() },
  { name: '福建', value: randomValue() },
  { name: '贵州', value: randomValue() },
  { name: '广东', value: randomValue() },
  { name: '青海', value: randomValue() },
  { name: '西藏', value: randomValue() },
  { name: '四川', value: randomValue() },
  { name: '宁夏', value: randomValue() },
  { name: '海南', value: randomValue() },
  { name: '台湾', value: randomValue() },
  { name: '香港', value: randomValue() },
  { name: '澳门', value: randomValue() }
]
function randomValue() {
  return Math.round(Math.random() * 1000);
}

class ListMap extends React.Component {
  constructor (props) {
    super (props)
    this.state = {
      citiesTable: [],
      citiesObj: {},
      // 各省确诊人数
      dataList: []
    }
  }
  render() {
    return (
      <div>
        <div className="map">
          <div id="map" className="map"></div>
        </div>
        <SearchCom citiesObj={this.state.citiesObj}></SearchCom>
        <table>
          <thead>
            <tr>
              <th>省份</th>
              <th>确诊人数</th>
              <th>死亡人数</th>
              <th>康复</th>
              <th>境外输入</th>
            </tr>
          </thead>
          <tbody>
            { this.state.citiesTable }
          </tbody>
        </table>
      </div>
    )
  }
  // 获取数据
  getCitiesList = () => {
    let dataList = []
    const chinaCity = yiqing.data.areaTree[2].children
    const citiesObj = {}
    const showCity = chinaCity.map((item, index) => {
      dataList.push({ name: item.name, value: item.total.confirm})
      citiesObj[item.name] = {confirm: item.total.confirm, dead: item.total.dead, heal: item.total.heal, input: item.total.input}
      return (
        <tr key={index}>
          <td>{item.name}</td>
          <td>{item.total.confirm}</td>
          <td>{item.total.dead}</td>
          <td>{item.total.heal}</td>
          <td>{item.total.input}</td>
        </tr>
      )
    })
    this.setState({
      citiesTable: showCity,
      citiesObj: citiesObj,
      dataList: dataList
    })
  }
  async componentDidMount() {
    await this.getCitiesList()
    var myChart = echarts.init(document.getElementById('map'))
    var option = {
      tooltip: {
        formatter: function (params, ticket, callback) {
          return params.seriesName + '<br />' + params.name + '：' + params.value
        }//数据格式化
      },
      visualMap: {
        min: 0,
        max: 1500,
        left: 'left',
        top: 'bottom',
        text: ['高', '低'],//取值范围的文字
        inRange: {
          color: ['#ffffff', '#555555']//取值范围的颜色
        },
        show: true//图注
      },
      geo: {
        map: 'china',
        roam: false,//不开启缩放和平移
        zoom: 1.23,//视角缩放比例
        label: {
          normal: {
            show: true,
            fontSize: '10',
            color: 'rgba(0,0,0,0.7)'
          }
        },
        itemStyle: {
          normal: {
            borderColor: 'rgba(0, 0, 0, 0.2)'
          },
          emphasis: {
            areaColor: '#F3B329',//鼠标选择区域颜色
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            shadowBlur: 20,
            borderWidth: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      },
      series: [
        {
          name: '信息量',
          type: 'map',
          geoIndex: 0,
          data: this.state.dataList
        }
      ]
    }
    myChart.setOption(option)
  }
}

ReactDOM.render(<ListMap />, document.getElementById('root'))
